﻿using Microsoft.SharePoint;
using System;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Linq;

namespace Portal
{
    [ToolboxItemAttribute(false)]
    public partial class Awards : WebPart
    {
        // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution
        // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
        // for production. Because the SecurityPermission attribute bypasses the security check for callers of
        // your constructor, it's not recommended for production purposes.
        // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
        public Awards()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            SPWeb web = SPContext.Current.Web;
            SPList awardList = web.GetList("/Lists/Awards/");

            SPQuery awardQuery = new SPQuery();

            awardQuery.Query = @"<OrderBy>
                             <FieldRef Name='DateOfEvent' Ascending='False' />
                            </OrderBy>";
            awardQuery.ViewFields = @"       <FieldRef Name='ID'/>
                                            <FieldRef Name='Title'/>
                                            <FieldRef Name='Description'/>                                            
                                            <FieldRef Name='DateOfEvent'/>
                                            <FieldRef Name='Category'/> 
                                            <FieldRef Name='Employee'/>    
                                            <FieldRef Name='AwardName'/>                                   
                                ";
            awardQuery.RowLimit = 5;

            SPListItemCollection awarditems = awardList.GetItems(awardQuery);

            if (awarditems.Count > 0)
            {
                var awardsItemsModified = from awardItem in awarditems.GetDataTable().AsEnumerable()
                                                 select new
                                                 {
                                                     AwardDate = Convert.ToDateTime(awardItem["DateOfEvent"].ToString()).ToString("MMM dd, yyyy"),
                                                     AwardDescription = awardItem["Description"].ToString().Length >= 200 ? awardItem["Description"].ToString().Substring(0, 200) + "..." : awardItem["Description"].ToString(),
                                                     AwardTitle = awardItem["Title"].ToString(),
                                                     AwardID = awardItem["ID"].ToString(),
                                                     AwardName = awardItem["AwardName"].ToString(),
                                                     AwardEmployee = awardItem["Employee"].ToString()

                                                 };

                lstAwards.DataSource = awardsItemsModified.ToList();
                lstAwards.DataBind();
            }
        }
    }
}
